<!DOCTYPE html>
<html lang="zh-CN" color-mode=light>


<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Windows BAT批处理由于CRLF问题引发的奇怪错误 - 树朾的开源技术分享</title>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate" />
  <meta name="keywords" content="开源, 分享">
  <meta name="description" content="问题非常奇怪，基本的日志输出都会出错，代码如下：">
  <meta name="author" content="scwang90">
  <link rel="icon" href="/images/icons/favicon.ico">
  
  
  
  
  

  
<link rel="stylesheet" href="/css/style.css">


  
    
<link rel="stylesheet" href="https://at.alicdn.com/t/font_1445822_p6ry5n7lrr.css">

  

  
    
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  

  
    
      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css" name="highlight-style" mode="light">

      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/solarized-dark.min.css" name="highlight-style" mode="dark">

      
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      loading: {
        gif: '/images/theme/loading.gif',
        lottie: ''
      },
      lazyload: {
        enable: true,
        only_post: 'false',
        loading: {
          gif: '/images/theme/loading.gif',
          lottie: ''
        }
      },
      donate: {
        enable: false,
        alipay: '/images/donate/pay_alipay.jpg',
        wechat: '/images/donate/pay_wxpay.jpg',
        tencent: '/images/donate/pay_tencent.jpg'
      },
      galleries: {
        enable: true
      },
      fab: {
        enable: true,
        always_show: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: false
      },
      preview: {
        background: {
          default: '',
          api: ''
        },
        motto: {
          default: '我在开了灯的床头下，想问问自己的心啊。',
          typing: true,
          api: 'https://v2.jinrishici.com/one.json',
          data_contents: '["data","content"]'
        },
      },
      qrcode: {
        enable: true,
        type: 'url',
        image: 'https://pic.izhaoo.com/weapp-code.jpg',
      },
      toc: {
        enable: true
      },
      scrollbar: {
        type: 'default'
      },
      notification: {
        enable: false,
        delay: 4500,
        list: '',
        page_white_list: '',
        page_black_list: ''
      },
      search: {
        enable: false,
        path: ''
      }
    }
  </script>

  

  

<meta name="generator" content="Hexo 6.2.0"></head>

<body class="lock-screen">
  <div class="loading" id="loading"></div>
  
    


  <nav class="navbar">
    <div class="left">
      
        <i class="iconfont iconhome j-navbar-back-home"></i>
      
      
        <i class="iconfont iconqrcode j-navbar-qrcode"></i>
      
      
        <i class="iconfont iconmoono" id="color-toggle" color-toggle="light"></i>
      
      
    </div>
    <div class="center">Windows BAT批处理由于CRLF问题引发的奇怪错误</div>
    <div class="right">
      <i class="iconfont iconmenu j-navbar-menu"></i>
    </div>
    
      <div id="qrcode-navbar"></div>
    
  </nav>

  
  

<nav class="menu">
  <div class="menu-container">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content"><li class="menu-item">
        <a href="/ " class="underline "> 首页</a>
      </li><li class="menu-item">
        <a href="/galleries/ " class="underline "> 摄影</a>
      </li><li class="menu-item">
        <a href="/archives/ " class="underline "> 归档</a>
      </li><li class="menu-item">
        <a href="/tags/ " class="underline "> 标签</a>
      </li><li class="menu-item">
        <a href="/categories/ " class="underline "> 分类</a>
      </li><li class="menu-item">
        <a href="/about/ " class="underline "> 关于</a>
      </li></ul>
    
      <div class="menu-copyright"><p>Powered by <a target="_blank" href="https://github.com/scwang90">scwang90</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p> <p><a target="_blank" href="http://beian.miit.gov.cn/"><span>黔ICP备2021004317号-1</span></a><span>  |  </span><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img class="lazyload" data-original="/images/icons/icon-filing.png" src="https://www.beian.gov.cn/img/new/gongan.png"/> <span> 贵公网安备 52010202002539号</span></a></p></div>
    
  </div>
</nav>
  <main id="main">
  <div class="article-wrap">
    
      <div class="row container container-lg">
        <div class="col-xl-2"></div>
        <div class="col-xl-8"><article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="/images/theme/post-image.jpg" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">Windows BAT批处理由于CRLF问题引发的奇怪错误</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>三月 30, 2024</span>
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>2355</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content article-entry">
        
          
            <p>问题非常奇怪，基本的日志输出都会出错，代码如下：</p>
          
        
        <h2 id="1-问题非常奇怪，基本的日志输出都会出错"><a href="#1-问题非常奇怪，基本的日志输出都会出错" class="headerlink" title="1. 问题非常奇怪，基本的日志输出都会出错"></a>1. 问题非常奇怪，基本的日志输出都会出错</h2><ul>
<li><p>代码如下：</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@<span class="built_in">echo</span> off  </span><br><span class="line"><span class="built_in">setlocal</span> enabledelayedexpansion  </span><br><span class="line">  </span><br><span class="line">:: 获取当前日期和时间，格式可能因系统区域设置而异  </span><br><span class="line"><span class="built_in">set</span> &quot;currentTime=<span class="variable">%date%</span> <span class="variable">%time%</span>&quot;  </span><br><span class="line">  </span><br><span class="line">:: 替换日期和时间格式中的特殊字符（如果需要）  </span><br><span class="line"><span class="built_in">set</span> &quot;currentTime=<span class="variable">!currentTime:/=-!</span>&quot;  </span><br><span class="line">  </span><br><span class="line">:: 输出当前时间加上消息内容到文件，如果文件不存在则创建  </span><br><span class="line"><span class="built_in">echo</span> [<span class="variable">!currentTime!</span>] 这是你的消息内容 &gt;&gt; output.log</span><br><span class="line">  </span><br><span class="line"><span class="built_in">endlocal</span></span><br></pre></td></tr></table></figure>
<p>代码没有什么功能实现，只是简单的输出日志到文件</p>
</li>
<li><p>运行结果：</p>
</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ ./speed-starter.bat</span><br><span class="line">&#x27;替换日期和时间格式中的特殊字符（如果需要）&#x27; is <span class="keyword">not</span> recognized as an internal or external command,</span><br><span class="line">operable program or batch file.</span><br><span class="line">&#x27;消息内容到文件，如果文件不存在则创建&#x27; is <span class="keyword">not</span> recognized as an internal or external command,</span><br><span class="line">operable program or batch file.</span><br><span class="line">&#x27;me!]&#x27; is <span class="keyword">not</span> recognized as an internal or external command,</span><br><span class="line">operable program or batch file.</span><br></pre></td></tr></table></figure>

<p>奇怪的错误，完全看不出问题的原因</p>
<h2 id="2-问题解决"><a href="#2-问题解决" class="headerlink" title="2. 问题解决"></a>2. 问题解决</h2><p>对于这个问题简直是毫无头绪，检测语法完全没有问题。最后为了确认语法没问题，使用VSCode新建另外一个代码内容完全一样的Bat文件：start.bat</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@<span class="built_in">echo</span> off  </span><br><span class="line"><span class="built_in">setlocal</span> enabledelayedexpansion  </span><br><span class="line">  </span><br><span class="line">:: 获取当前日期和时间，格式可能因系统区域设置而异  </span><br><span class="line"><span class="built_in">set</span> &quot;currentTime=<span class="variable">%date%</span> <span class="variable">%time%</span>&quot;  </span><br><span class="line">  </span><br><span class="line">:: 替换日期和时间格式中的特殊字符（如果需要）  </span><br><span class="line"><span class="built_in">set</span> &quot;currentTime=<span class="variable">!currentTime:/=-!</span>&quot;  </span><br><span class="line">  </span><br><span class="line">:: 输出当前时间加上消息内容到文件，如果文件不存在则创建  </span><br><span class="line"><span class="built_in">echo</span> [<span class="variable">!currentTime!</span>] 这是你的消息内容 &gt;&gt; output.log</span><br><span class="line">  </span><br><span class="line"><span class="built_in">endlocal</span></span><br></pre></td></tr></table></figure>

<p>运行发现没有任何错误，日志也输出到了 output.log<br>于是重新运行之前的 starter.bat 还是相同的错误！这就奇怪了！<br>为了验证两个文件的差别，用 VSCode 同时左右分屏打开，代码内容确实完全一样！还是毫无头绪。</p>
<p>最终不经意注意到了 VSCode 在切换两个文件时，右下角的状态栏由变化：</p>
<ul>
<li>starter.bat -&gt; LF</li>
<li>start.bat -&gt; CRLF</li>
</ul>
<p>CRLF 我以前有了解过，就是不同操作系统对文本文件的换行符号有所不同，Windows 下的换行是 <code>\r\n</code>，也叫回车换行，对应 <code>CRLF</code>。<br>所以 starter.bat 只有 <code>LF</code> 没有 <code>RC</code>，脚本解释器把所有代码都认为在同一行执行，出错时当然的了！</p>
<p>所以解决办法就是把 <code>LF</code> 切换到 <code>CRLF</code> 模式。这得借助 VSCode 或者 IDEA 这样的高级编辑器，记事本估计不行。</p>
<h2 id="3-CRLF-知识补课"><a href="#3-CRLF-知识补课" class="headerlink" title="3. CRLF 知识补课"></a>3. CRLF 知识补课</h2><p>CRLF是“Carriage Return Line Feed”的缩写，它表示回车换行，对应的ASCII码是“\r\n”。</p>
<p>关于CRLF，以及不同操作系统之间的差异，以下是一些相关知识：</p>
<ul>
<li><ol>
<li><em>CR、LF与CRLF的含义与起源</em>：</li>
</ol>
<ul>
<li>CR（Carriage Return，回车）：对应ASCII中的转义字符’\r’，其十进制ASCII代码是13，十六进制代码为0x0D。在机械打字机时代，CR的作用是将打字机上的滚动托架（Carriage）滚回到打印纸张的最左侧，但保持当前打字的垂直位置不变，即还是在同一行。</li>
<li><em>LF</em>（Line Feed，换行）：对应ASCII中的转义字符’\n’，其ASCII代码是10，十六进制为0x0A。LF的作用是将打印纸张上移一行位置，但保持当前打字的水平位置不变。</li>
<li>CRLF：即CR+LF，表示回车并换行，对应的ASCII码是“\r\n”。</li>
</ul>
</li>
<li><ol start="2">
<li><em>不同操作系统中的换行符差异</em>：</li>
</ol>
<ul>
<li><p>Windows系统：采用CRLF（\r\n）表示回车换行。这意味着在Windows平台上，换行在文本文件中是使用两个字节（0d 0a）来表示的。</p>
</li>
<li><p>Unix&#x2F;Linux系统：采用LF（\n）表示换行。在Unix&#x2F;Linux系统中，每行的末尾只有newline（即“\n”），换行符由0a一个字节表示。</p>
</li>
<li><p>Mac系统：早期的Mac OS系统（如MacIntosh）采用CR（\r）表示回车作为换行；而现代的Mac OS X系统则与其他 Unix-like 系统一样，采用LF（\n）作为换行符。</p>
</li>
</ul>
</li>
<li><ol start="2">
<li><em>换行符差异的影响</em>：这种差异在文本编辑和跨平台软件开发中可能会导致问题。例如，当代码开发者在不同的系统上都编辑过同一份代码并来回传输时，或者当多人协作开发一个项目并使用版本控制系统（如git）进行源码管理时，就可能会出现换行符不统一的问题。这可能会导致代码在不同系统上的表现不一致，或者在版本控制系统中产生不必要的差异。</li>
</ol>
</li>
</ul>

      </section>
      <section class="extra">
        
          <ul class="copyright">
  
    <li><strong>本文作者：</strong>scwang90</li>
    <li><strong>本文链接：</strong><a href="https://blog.scwang90.cn/2024/03/30/bat-crlf/index.html" title="https:&#x2F;&#x2F;blog.scwang90.cn&#x2F;2024&#x2F;03&#x2F;30&#x2F;bat-crlf&#x2F;index.html">https:&#x2F;&#x2F;blog.scwang90.cn&#x2F;2024&#x2F;03&#x2F;30&#x2F;bat-crlf&#x2F;index.html</a></li>
    <li><strong>版权声明：</strong>本分享所有文章均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" title="BY-NC-SA" target="_blank" rel="noopener">BY-NC-SA</a> 许可协议，转载请注明出处！</li>
  
</ul>
        
        
        
  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/BAT%E6%89%B9%E5%A4%84%E7%90%86/" rel="tag">BAT批处理</a></li></ul> 

        
  <nav class="nav">
    <a href="/2024/04/07/js-string-sub/"><i class="iconfont iconleft"></i>JS 中 String 的字符串截取 substr 和 substring 区别的快速记忆方法</a>
    <a href="/2024/03/22/ssh-bashrc/">SSH 登录后，.bashrc  文件没有被加载问题探究与解决<i class="iconfont iconright"></i></a>
  </nav>

      </section>
      
    </section>
  </div>
</article></div>
        <div class="col-xl-2">
          
            
  <aside class="toc-wrap">
    <h3 class="toc-title">文章目录：</h3>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E9%97%AE%E9%A2%98%E9%9D%9E%E5%B8%B8%E5%A5%87%E6%80%AA%EF%BC%8C%E5%9F%BA%E6%9C%AC%E7%9A%84%E6%97%A5%E5%BF%97%E8%BE%93%E5%87%BA%E9%83%BD%E4%BC%9A%E5%87%BA%E9%94%99"><span class="toc-text">1. 问题非常奇怪，基本的日志输出都会出错</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3"><span class="toc-text">2. 问题解决</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-CRLF-%E7%9F%A5%E8%AF%86%E8%A1%A5%E8%AF%BE"><span class="toc-text">3. CRLF 知识补课</span></a></li></ol>
  </aside>

          
        </div>
      </div>
    
  </div>
</main>
  

<footer class="footer">
  <div class="footer-social"><a 
        href="tencent://message/?Menu=yes&uin=283371828 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#12B7F5'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconQQ "></i>
      </a><a 
        href="javascript:; "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#09BB07'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconwechat-fill "></i>
      </a><a 
        href="https://github.com/scwang90 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#9f7be1'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  icongithub-fill "></i>
      </a><a 
        href="mailto:scwang90@hotmail.com "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color=#FF3B00" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconmail"></i>
      </a></div>
  
    <div class="footer-copyright"><p>Powered by <a target="_blank" href="https://github.com/scwang90">scwang90</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p> <p><a target="_blank" href="http://beian.miit.gov.cn/"><span>黔ICP备2021004317号-1</span></a><span>  |  </span><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img class="lazyload" data-original="/images/icons/icon-filing.png" src="https://www.beian.gov.cn/img/new/gongan.png"/> <span> 贵公网安备 52010202002539号</span></a></p></div>
  
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
  
  
    
<script src="/js/color-mode.js"></script>

  
  
</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>





  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




  
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>






  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/script.js"></script>







  <script>
    (function () {
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>













</html>